package com.lemon.rpcframe.dao.basedao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;

/**
 * 
 * @author tangli
 */
public class DBConnectionUtil {
    private static Logger logger = Logger.getLogger(DBConnectionUtil.class);
    private static DBConnectionUtil instance = null;
    private static Object _lock = new Object();

    public static DBConnectionUtil getInstance() {
        if (instance == null) {
            synchronized (_lock) {
                if (instance == null) {
                    instance = new DBConnectionUtil();
                }
            }
        }
        return instance;
    }

    public Connection getConnection() throws SQLException, IOException {
        BasicDataSource ds = null;
        if (ds == null) {
            synchronized (this) {
                if (ds == null) {
                    ds = newDataSource();
                }
            }
        }
        Connection conn = null;
        try {
            conn = ds.getConnection();
            return conn;
        } catch (Exception e) {
            throw new SQLException("no idle connection: " + e.getLocalizedMessage());
        }
    }

    public void execSql(String sql) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            returnConnection(conn);
        }
    }

    /**
     * 归还连接
     * 
     * @param conn
     *            Connection
     */
    public void returnConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
            }
        }
    }

    private BasicDataSource newDataSource() throws SQLException, IOException {
        return initDataSource();
    }

    private BasicDataSource initDataSource() throws SQLException, IOException {
        String url = "jdbc:mysql://10.9.18.86:3306/tableau?connectTimeout=1000&useUnicode=true&characterEncoding=utf-8";
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUsername("tableau");
        ds.setPassword("H8aKedqwmpTpnGLW");
        ds.setUrl(url);
        ds.setMaxActive(4);
        ds.setMaxIdle(0);
        ds.setMinIdle(0);
        ds.setMaxWait(60 * 1000);

        logger.info("Create new connection for " + " with url=" + url);
        return ds;
    }
}
